package com.xkdx.caipiao.my;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.xkdx.caipiao.MyLotterActivity;
import com.xkdx.caipiao.R;
import com.xkdx.caipiao.ThreadActivity;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryAction2;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryInfo;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryModule2;
import com.xkdx.caipiao.presistence.historyquery.HistoryQueryPresistence2;
import com.xkdx.caipiao.presistence.hongbao.HongbaoInfo;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.util.SharePrefenceUtil;

public class LotteryDetailActivity extends ThreadActivity implements
		OnClickListener, OnPageChangeListener {
	private SharePrefenceUtil su;
	public static boolean isUpdate = false;
	private static Context context;
	View view;
	TextView title_info, red_detail, buy_lottery_tip, account;
	ViewPager viewPager;
	LinearLayout ll;

	HistoryQueryAction2 action;
	HistoryQueryModule2 module;
	HistoryQueryPresistence2 presistence;

	ListView lv_all, lv_no_open, lv_win;
	View view_all, view_no_open, view_win;
	List<View> viewList;
	List<HistoryQueryInfo> list, list_win, list_no_open;
	List<HongbaoInfo> list1;

	Drawable im_bottom;
	TextView tv_all, tv_win, tv_no_open, yuan;
	Button btn_recharge, btn_withdraw_cash;
	LoginInfo info;
	HongbaoInfo info1;
	float money;

	Button redpacke;
	int clickpage;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lotterydetail_caipiao);
		im_bottom = getResources().getDrawable(R.drawable.viewpagger_line_caipiao);
		im_bottom.setBounds(0, 0, im_bottom.getMinimumWidth(),
				im_bottom.getMinimumHeight());
		findView();
		setonClick();
		startThread();
	}

	private void setonClick() {
		tv_all.setOnClickListener(this);
		tv_win.setOnClickListener(this);
		tv_no_open.setOnClickListener(this);

	}

	private void findView() {
		tv_all = (TextView) findViewById(R.id.text1);
		tv_win = (TextView) findViewById(R.id.text2);
		tv_no_open = (TextView) findViewById(R.id.text3);

		buy_lottery_tip = (TextView) findViewById(R.id.buy_lottery_tip);
		ll = (LinearLayout) findViewById(R.id.ll_buy_lottery);

		viewPager = (ViewPager) findViewById(R.id.vPager);
		LayoutInflater lf = LayoutInflater.from(LotteryDetailActivity.this);
		list_win = new ArrayList<HistoryQueryInfo>();
		list_no_open = new ArrayList<HistoryQueryInfo>();

		view_all = lf.inflate(R.layout.all_order_caipiao, null);
		view_no_open = lf.inflate(R.layout.noopen_order_caipiao, null);
		view_win = lf.inflate(R.layout.win_order_caipiao, null);

		viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
		viewList.add(view_all);
		viewList.add(view_win);
		viewList.add(view_no_open);

		lv_all = (ListView) view_all.findViewById(R.id.all_order_ls);
		lv_no_open = (ListView) view_no_open
				.findViewById(R.id.no_open_order_ls);
		lv_win = (ListView) view_win.findViewById(R.id.win_order_ls);

		viewPager.setAdapter(new adapter());
		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(this);
	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		dataLoadDialog.dismiss();

		if (result.containsKey("0")) {

			list = module.list;
			if (list != null && list.size() > 0) {
				viewPager.setVisibility(View.VISIBLE);
				buy_lottery_tip.setVisibility(View.GONE);
				setList();
			} else {
				viewPager.setVisibility(View.GONE);
				buy_lottery_tip.setVisibility(View.VISIBLE);
			}

		}
	}

	private void setList() {

		for (HistoryQueryInfo info : list) {
			if (info.getStatus().equals("0")) {
				list_no_open.add(info);
			}
			if (info.getStatus().equals("2")) {
				list_win.add(info);
			}
		}

		listAdapter adapter = new listAdapter(list);
		lv_all.setAdapter(adapter);
		lv_all.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {

				Intent intent = new Intent(LotteryDetailActivity.this,
						OrderDetailsActivity.class);
				intent.putExtra("orderid", list.get(position).getOrderid());

				startActivity(intent);
			}
		});
		listAdapter noOpenadapter = new listAdapter(list_no_open);
		lv_no_open.setAdapter(noOpenadapter);

		lv_no_open.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {

				Intent intent = new Intent(LotteryDetailActivity.this,
						OrderDetailsActivity.class);
				intent.putExtra("orderid", list_no_open.get(position)
						.getOrderid());

				startActivity(intent);
			}
		});
		listAdapter winAdapter = new listAdapter(list_win);
		lv_win.setAdapter(winAdapter);
		lv_win.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {

				Intent intent = new Intent(LotteryDetailActivity.this,
						OrderDetailsActivity.class);
				intent.putExtra("orderid", list_win.get(position).getOrderid());

				startActivity(intent);
			}
		});
	}


	@Override
	public void startThread() {
		if (!(dataLoadDialog.isShowing())) {
			dataLoadDialog.show();
			dataLoadDialog.setContentView(R.layout.progressbar_caipiao);
		}
		LoginInfo info = getLoginInfo();
		action = new HistoryQueryAction2(info.getId(), info.getLogintoken(),
				"all", "1", "1000");
		module = new HistoryQueryModule2(LotteryDetailActivity.this);
		presistence = new HistoryQueryPresistence2(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();

	}

	class adapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 3;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {

			container.removeView(viewList.get(position));

		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(viewList.get(position));// 添加页卡
			return viewList.get(position);
		}

	}

	class listAdapter extends BaseAdapter {
		List<HistoryQueryInfo> list;

		public listAdapter(List<HistoryQueryInfo> list) {
			// TODO Auto-generated constructor stub
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertview, ViewGroup arg2) {
			viewHolder holder;
			if (convertview == null) {
				holder = new viewHolder();
				convertview = LayoutInflater.from(LotteryDetailActivity.this)
						.inflate(R.layout.order_query_item_caipiao, null);
				holder.name = (TextView) convertview.findViewById(R.id.name);
				holder.pay = (TextView) convertview.findViewById(R.id.pay);
				holder.time = (TextView) convertview.findViewById(R.id.time);
				holder.states = (TextView) convertview
						.findViewById(R.id.status);
				convertview.setTag(holder);
			} else {
				holder = (viewHolder) convertview.getTag();
			}

			holder.name.setText(list.get(position).getLotteryname());
			float mon = Float.valueOf(list.get(position).getOrderpaymoney()) / 100;
			DecimalFormat df = new DecimalFormat("#.00");
			String money = df.format(mon);

			holder.pay.setText(money + "元");
			holder.time.setText(list.get(position).getCtime());
			String status = list.get(position).getStatus();
			String tradestatus = list.get(position).getTradestatus();
			String issurepay = list.get(position).getIssurepay();
			String outime = list.get(position).getOuttime();
			if (outime.equals("false")) {
				if (issurepay.equals("0")) {
					holder.states.setText("未支付");
				} else {
					if (tradestatus.equals("1")) {
						holder.states.setText("出票中");
					} else if (tradestatus.equals("2")) {
						holder.states.setText("出票中");
					} else if (tradestatus.equals("3")) {
						if (status.equals("0")) {
							holder.states.setText("待开奖");
						} else if (status.equals("1")) {
							holder.states.setText("未中奖");
						} else {
							holder.states.setText("中奖");
						}
					} else if ("4".equals(tradestatus)) {
						holder.states.setText("出票失败");
					} else {
						holder.states.setText("部分出票失败");
					}
				}
			} else {
				holder.states.setText("已过期");
			}
			return convertview;
		}

		class viewHolder {
			TextView name, pay, time, states;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int id) {
		switch (id) {
		case 0:
			tv_all.setCompoundDrawables(null, null, null, im_bottom);
			tv_no_open.setCompoundDrawables(null, null, null, null);
			tv_win.setCompoundDrawables(null, null, null, null);

			break;
		case 1:
			tv_all.setCompoundDrawables(null, null, null, null);
			tv_no_open.setCompoundDrawables(null, null, null, null);
			tv_win.setCompoundDrawables(null, null, null, im_bottom);

			break;
		case 2:
			tv_all.setCompoundDrawables(null, null, null, null);
			tv_no_open.setCompoundDrawables(null, null, null, im_bottom);
			tv_win.setCompoundDrawables(null, null, null, null);

			break;
		default:
			break;
		}

	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.text1:
			viewPager.setCurrentItem(0);
			break;
		case R.id.text2:
			viewPager.setCurrentItem(1);
			break;
		case R.id.text3:
			viewPager.setCurrentItem(2);
			break;

		default:
			break;
		}

	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

	}

	@Override
	public void onBackClick(View view) {
		finish();
	}

}
